package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CatgoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/admin/category")
@RestController
public class CategoryController {
    @Autowired
    private CatgoryService catgoryService;
    @GetMapping("/page")
    public Result<PageResult> pageCategory( CategoryPageQueryDTO categoryPageQueryDTO){
       PageResult pageResult= catgoryService.pageCategory(categoryPageQueryDTO);

return Result.success(pageResult);
    }
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO){

        catgoryService.add(categoryDTO);

        return Result.success();
    }
   @PostMapping("/status/{status}")
    public Result openOrClose(@PathVariable Integer status, Long id){
        catgoryService.openOrClose(status,id);
        return Result.success();
   }

   @PutMapping
   @CacheEvict()
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
       catgoryService.updateCategory(categoryDTO);
        return Result.success();
   }
@DeleteMapping
    public Result DeleteById(Long id){
        catgoryService.DeleteById(id);
        return Result.success();
}

@GetMapping("list")
    public Result<List<Category>> list(Short type){
        List<Category> list=catgoryService.list(type);
        return Result.success(list);


}


}
